home *** CD-ROM | disk | FTP | other *** search
- #ifndef indent
- static char rcsid[] = "$Id: pgmhisteq.c,v 1.2 1993/03/22 09:25:45 amoss Exp amoss $";
- #endif
-
- /*
- * Apply histogram equalization to a PGM file.
- */
-
- /*#include <malloc.h>*/
- #include <stdlib.h>
- #include <pgm.h>
-
- void main (ac, av)
- int ac;
- char **av;
- {
- FILE *ifp;
- gray maxval, **image;
- register gray *gP;
- double P;
- int rows, cols, sum, i, j, *hist, *newhist;
-
- pgm_init (&ac, av);
-
- switch (ac) {
-
- case 1:
- ifp = stdin;
- break;
-
- case 2:
- ifp = pm_openr (av[1]);
- break;
-
- default:
- pm_usage ("[pgmfile]");
- }
-
- image = pgm_readpgm (ifp, &cols, &rows, &maxval);
-
- pm_close (ifp);
-
- if (! (hist = (int *) calloc (maxval + 1, sizeof (int))))
- pm_error ("out of memory (input histogram)");
-
- if (! (newhist = (int *) malloc ((maxval + 1) * sizeof (int))))
- pm_error ("out of memory (output histogram)");
-
- /* Build histogram. */
- for (i = 0; i < rows; ++i)
- for (j = 0, gP = image[i]; j < cols; ++j, ++gP)
- ++hist[(int) *gP];
-
- P = (double) (maxval + 1) / (double) (rows * cols);
-
- newhist[0] = (int) P;
-
- for (i = 1, sum = hist[0]; i < maxval; ++i) {
- newhist[i] = (int) (P * sum);
-
- sum += hist[i];
- }
-
- for (i = 0; i < rows; ++i)
- for (j = 0, gP = image[i]; j < cols; ++j, ++gP)
- *gP = (gray) newhist[(int) *gP];
-
- pgm_writepgm (stdout, image, cols, rows, maxval, 0);
-
- exit (0);
- }
-
-